<?php

/**
 * Description of Reranking
 *
 * @author Pavel Homolka
 */
class RerankingPresenter extends BasePresenter {

    /**
     *
     * @var Reranking
     */
    private $reranking;

    /**
     *
     * @var Flickr
     */
    protected $flickr;


    private $result;

    public function startup() {
        parent::startup();

        $this->flickr = $this->context->flickr;
        $this->reranking = $this->context->reranking;
        //$this->photosSession = $this->context->photosSession;
    }

    public function renderShowResult() {
        $this->template->result = $this->photosSession->getResultingPhotos();
        $this->template->countResult = count($this->photosSession->getResultingPhotos());
        $this->template->minLevenshteinDistance = $this->photosSession->getMinLevenshteinDistance();
        $this->template->minGreatCircleDistance = $this->photosSession->getMinGreatCircleDistance();
        $this->template->communication = $this->photosSession->getCommunication();
        //dump($this->photosSession->getCommunication());
        //dump($this->photosSession->getResultingPhotos());
        //dump($this->photosSession->getMinGreatCircleDistance());
    }

    public function createComponentSearchForm() {
        $form = new SearchForm();
        $form->onSuccess[] = callback($this, 'seachFormSubmitted');
        return $form;
    }

    public function seachFormSubmitted($form) {
        $vales = $form->getValues();
        if (empty($vales['gps'])) {
            $this->result = $this->flickr->search($vales['keyword'], $vales['numberResult']);
        } else {
            $coordinates = explode(';', $vales['gps']);
            $this->result = $this->flickr->search($vales['keyword'], $vales['numberResult'], $coordinates[0], $coordinates[1], $vales['radius']);
        }
    }

    public function createComponentRerankingForm() {
        $form = new RerankingForm();
        $form->onSuccess[] = callback($this, 'rerankingFormSubmitted');
        return $form;
    }

    public function rerankingFormSubmitted($form) {
        $valeus = $form->getValues();
        $psotos = $this->photosSession->getResultingPhotos();

        if (!empty($valeus['autor']))
            $this->reranking->levenshteinDistance('ownername', $valeus['autor'], $valeus['autorRadius']);

        if (!empty($valeus['title']))
            $this->reranking->levenshteinDistance('title', $valeus['title'], $valeus['titleRadius']);

        if (!empty($valeus['tag']))
            $this->reranking->levenshteinDistance('tags', $valeus['tag'], $valeus['tagRadius']);

        if (!empty($valeus['gps'])) {
            $coordinates = explode(';', $valeus['gps']);

            $latitude = $coordinates[0];
            $longitude = $coordinates[1];

            $this->reranking->greatCircleDistance($latitude, $longitude, $valeus['gpsRadius']);
        }

        if (!empty($valeus['dateUpload'])) {
            $this->reranking->dateDistance('dateupload', $valeus['dateUpload'], $valeus['dateUploadRadius']);
        }

        if (!empty($valeus['dateTaken'])) {
            $this->reranking->dateDistance('datetaken', $valeus['dateTaken'], $valeus['dateTakenRadius']);
        }
    }

}